﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Collections;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text.RegularExpressions;
namespace Agro.Web.Areas.DataManage.Controllers
{
    public class ImageSaveController : Controller
    {
   
        String data;
      
        String[] rows;
       
        int width;
        int height;
       
        String bgcolor;
        Color bgColor;
       
        Bitmap chart;
        
        Graphics gr;
        public ActionResult Index()
        {
            try
            {
              
                width = Int32.Parse(Request["meta_width"]);
                height = Int32.Parse(Request["meta_height"]);
            }
            catch (Exception ex)
            {
                
                Response.Write("Image width and height not provided.");
                Response.End();
            }
           
            bgcolor = Request["meta_bgColor"];
            if (bgcolor == "" || bgcolor == null)
            {
                bgcolor = "FFFFFF";
            }
          
            bgColor = ColorTranslator.FromHtml("#" + bgcolor);

           
            data = Request["stream"];

           
            rows = new String[height + 1];
            rows = data.Split(';');

          
            buildBitmap();

          
            Response.ContentType = "image/jpeg";
            Response.AddHeader("Content-Disposition", "attachment; filename=\"oyCharts.jpg\"");

           
            ImageCodecInfo[] encoders;
            ImageCodecInfo img_encoder = null;
            encoders = ImageCodecInfo.GetImageEncoders();
            foreach (ImageCodecInfo codec in encoders)
            {
                if (codec.MimeType == Response.ContentType)
                {
                    img_encoder = codec;
                    break;
                }
            }
            
            EncoderParameter jpeg_quality = new EncoderParameter(Encoder.Quality, 100L);
            EncoderParameters enc_params = new EncoderParameters(1);
           
            enc_params.Param[0] = jpeg_quality;

         
            chart.Save(Response.OutputStream, img_encoder, enc_params);

            gr.Dispose();
            chart.Dispose();

            return View();
        }

        private void buildBitmap()
        {
            chart = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            gr = Graphics.FromImage(chart);
            gr.Clear(bgColor);
            String c;
            int r;
            int ri = 0;
            for (int i = 0; i < rows.Length; i++)
            {
               	
                String[] pixels = rows[i].Split(',');
              
                ri = 0;
                for (int j = 0; j < pixels.Length; j++)
                {
                
                    String[] clrs = pixels[j].Split('_');
                 
                    c = clrs[0];
                    r = Int32.Parse(clrs[1]);
             
                    if (c != "")
                    {
                        if (c.Length < 6)
                        {
                            c = c.PadLeft(6, '0');
                        }
                        for (int k = 1; k <= r; k++)
                        {
                            chart.SetPixel(ri, i, ColorTranslator.FromHtml("#" + c));
                          
                            ri++;
                        }
                    }
                    else
                    {
                      
                        ri = ri + r;
                    }
                }
            }
        }

    }
}
